import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-10-08
 * Time: 12:08
 */
public class letter_Combinations {

    String[] char_abc = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    StringBuffer path;
    List<String> ret;

    public List<String> letterCombinations(String digits) {
        ret = new ArrayList<>();
        path = new StringBuffer();
        if (digits.isEmpty()) {
            return ret;
        }
        letter_dfs(digits, 0);
        return ret;
    }

    private void letter_dfs(String digits, int pos) {
        if (pos == digits.length()) {
            ret.add(path.toString());
            return;
        }
        int n = digits.charAt(pos) - '0' - 1;
        String str = char_abc[n];
        for (int i = 0; i < str.length(); i++) {
            path.append(str.charAt(i));
            letter_dfs(digits, pos + 1);
            path.deleteCharAt(path.length() - 1);
        }
    }

}
